<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-16
 * Time: 下午4:52
 */
namespace Addons\Controller;
use Think;
abstract class Addon {
	protected $view = null;
	public function __construct(){
		//Vendor('Smarty.Smarty', '', '.class.php');
		$this->view = new \Think\View();
		//$this->view->template_dir = realpath(APP_ROOT.APP_PATH.'Addons/View/');
		//$this->view->compile_dir = CACHE_PATH.'Addons/';
		//$this->view->config_dir = realpath(CONF_PATH);
	}

	/**
	 * 插件模板显示方法
	 * @param string $template
	 */
	final protected function display($template=''){
		echo ($this->fetch($template));
	}

	/**
	 * 模板变量赋值
	 * @access protected
	 * @param mixed $name 要显示的模板变量
	 * @param mixed $value 变量的值
	 * @return Action
	 */
	final protected function assign($name,$value='') {
		if(is_array($name))
			$this->view->assign($name);
		else
			$this->view->assign($name,$value);
		return $this;
	}


	//用于显示模板的方法
	final protected function fetch($templateFile = ''){
		if($templateFile == '')
			$templateFile = realpath(APP_ROOT.T('Addons@Index:index'));
		else
			$templateFile = realpath(APP_ROOT.T('Addons@'.$templateFile));
		if(!is_file($templateFile)){
			$templateFile = $this->addon_path.$templateFile.C('TMPL_TEMPLATE_SUFFIX');
			if(!is_file($templateFile)){
				throw new \Exception("模板不存在:$templateFile");
			}
		}
		return $this->view->fetch($templateFile);
	}
} 